<template>
  <HomePanel ref="target" title="新鲜好物" sub-title="新鲜出炉 品质靠谱">
    <template #right>
      <xtx-more></xtx-more>
    </template>
    <template #default>
      <!-- <ul ref="pannel" class="goods-list">
        <li v-for="item in prdList" :key="item.id">
          <RouterLink to="/">
            <img :src="item.picture" alt="" />
            <p class="name">{{ item.name }}</p>
            <p class="price">&yen;{{ item.price }}</p>
          </RouterLink>
        </li>
      </ul> -->
      <HomeList :goods="prdList"/>
    </template>
  </HomePanel>
</template>

<script>
import HomePanel from './home-panel'
import HomeList from './home-list'
import { findNew } from '@/api/home'
import { ref } from 'vue'
// 导入监控元素是否进入可视区函数
import { useObserver } from '@/hooks'

// vue3函数式编程代码优化提取出来
const useNew = () => {
  // 商品列表
  const prdList = ref([])
  // 获取商品列表
  const getPrdList = async () => {
    const { result } = await findNew()
    // console.log(reuslt)
    prdList.value = result
  }

  // 挂载更新时
  // onMounted(() => {
  //   getPrdList()
  // })

  return { prdList, getPrdList }
}

export default {
  components: {
    HomePanel,
    HomeList
  },
  setup () {
    // 调用我们提取出去的优化代码需要结构出来
    const { prdList, getPrdList } = useNew()
    // 调用监控函数
    const { target } = useObserver(getPrdList)

    return { prdList, target }
  }
}
</script>

<style scoped lang='less'>

</style>
